<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
  Module: FileIO
  
    &mdash; FSelector Documentation
  
</title>

  <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />

  <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />

<script type="text/javascript" charset="utf-8">
  relpath = '';
  if (relpath != '') relpath += '/';
</script>

  <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>

  <script type="text/javascript" charset="utf-8" src="js/app.js"></script>


  </head>
  <body>
    <script type="text/javascript" charset="utf-8">
      if (window.top.frames.main) document.body.className = 'frames';
    </script>
    
    <div id="header">
      <div id="menu">
  
    <a href="_index.html">Index (F)</a> &raquo; 
    
    
    <span class="title">FileIO</span>
  
  
  <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
</div>

      <div id="search">
  
    <a id="class_list_link" href="#">Class List</a>
  
    <a id="method_list_link" href="#">Method List</a>
  
    <a id="file_list_link" href="#">File List</a>
  
</div>
      <div class="clear"></div>
    </div>
    
    <iframe id="search_frame"></iframe>
    
    <div id="content"><h1>Module: FileIO
  
  
  
</h1>

<dl class="box">
  
  
    
  
    
  
  
    <dt class="r1">Included in:</dt>
    <dd class="r1"><span class='object_link'><a href="FSelector/Base.html" title="FSelector::Base (class)">FSelector::Base</a></span></dd>
    
  
  
    <dt class="r2 last">Defined in:</dt>
    <dd class="r2 last">lib/fselector/fileio.rb</dd>
  
</dl>
<div class="clear"></div>

<h2>Overview</h2><div class="docstring">
  <div class="discussion">
    
  <div class="note notetag">
    <strong>Note:</strong> 
    <div class='inline'><p>class labels and features are treated as symbols</p>
</div>
  </div>

<p>read and write various file formats, 
the internal data structure looks like:  </p>

<pre class="code ruby"><code>data = {

  :c1 =&gt; [                             # class c1
    {:f1=&gt;1, :f2=&gt;2}                   # sample 2
  ],

  :c2 =&gt; [                             # class c2
    {:f1=&gt;1, :f3=&gt;3},                  # sample 1
    {:f2=&gt;2}                           # sample 3
  ]

}

where :c1 and :c2 are class labels; :f1, :f2, and :f3 are features
</code></pre>


  </div>
</div>
<div class="tags">
  

</div>






  
    <h2>
      Instance Method Summary
      <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#data_from_csv-instance_method" title="#data_from_csv (instance method)">- <strong>data_from_csv</strong>(fname = :stdin, csv_opts = {}) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>read from csv.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#data_from_libsvm-instance_method" title="#data_from_libsvm (instance method)">- <strong>data_from_libsvm</strong>(fname = :stdin) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>read from libsvm.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#data_from_random-instance_method" title="#data_from_random (instance method)">- <strong>data_from_random</strong>(nsample = 100, nclass = 2, nfeature = 10, ncategory = 2, allow_mv = true) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>read from random data (read only, for test purpose).</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#data_from_url-instance_method" title="#data_from_url (instance method)">- <strong>data_from_url</strong>(url, format, *args) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>read data from url.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#data_from_weka-instance_method" title="#data_from_weka (instance method)">- <strong>data_from_weka</strong>(fname = :stdin, quote_char = '&quot;') </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>read from WEKA ARFF file.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#data_to_csv-instance_method" title="#data_to_csv (instance method)">- <strong>data_to_csv</strong>(fname = :stdout) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>write to csv.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#data_to_libsvm-instance_method" title="#data_to_libsvm (instance method)">- <strong>data_to_libsvm</strong>(fname = :stdout) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>write to libsvm.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#data_to_weka-instance_method" title="#data_to_weka (instance method)">- <strong>data_to_weka</strong>(fname = :stdout, format = nil) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>write to WEKA ARFF file.</p>
</div></span>
  
</li>

      
    </ul>
  



  <div id="instance_method_details" class="method_details_list">
    <h2>Instance Method Details</h2>
    
    
      <div class="method_details first">
  <p class="signature first" id="data_from_csv-instance_method">
  
    - <strong>data_from_csv</strong>(fname = :stdin, csv_opts = {}) 
  

  
</p><div class="docstring">
  <div class="discussion">
    
  <div class="note notetag">
    <strong>Note:</strong> 
    <div class='inline'><p>missing values are allowed</p>
</div>
  </div>

<p>read from csv</p>

<p>if no csv_opts supplied, we assume the CSV file in the following format:<br>
first row contains feature names with last column being class name </p>

<pre class="code ruby"><code>feat_name1,feat_name2,...,class_name
</code></pre>

<p>second row contains feature types with last column being class type</p>

<pre class="code ruby"><code>feat_type1,feat_type2,...,class_type  
</code></pre>

<p>and the remaing rows containing feature values with last column being class label </p>

<pre class="code ruby"><code>feat_value1,feat_value2,...,class_label  
...  
</code></pre>

<p>allowed feature types (case-insensitive) are:<br>
INTEGER, REAL, NUMERIC, CONTINUOUS, DOUBLE, FLOAT, STRING, NOMINAL, CATEGORICAL</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>fname</span>
      
      
        <span class='type'>(<tt>Symbol|<span class='object_link'><a href="String.html" title="String (class)">String</a></span>|StringIO</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>:stdin</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>data source to read from<br>
:stdin  # read from standard input</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>csv_opts</span>
      
      
        <span class='type'>(<tt>Hash</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>{}</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>named arguments for csv options<br>
:feature_name_row =&gt; 1,    # (first) row that contains feature names<br>
:feature_type_row =&gt; 2,    # (second) row that contains feature types<br>
:class_label_column =&gt; 0   # (last) column that contains class labels<br>
:feature_name2type =&gt; {},   # user-supplied hash containing feature name-type pairs 
                               if no rows specify them. feature must be in the same order 
                               as it appears in the dataset</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/fileio.rb', line 190</span>

<span class='kw'>def</span> <span class='id identifier rubyid_data_from_csv'>data_from_csv</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='op'>=</span><span class='symbol'>:stdin</span><span class='comma'>,</span> <span class='id identifier rubyid_csv_opts'>csv_opts</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_ifs'>ifs</span> <span class='op'>=</span> <span class='id identifier rubyid_get_ifs'>get_ifs</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='rparen'>)</span>
  
  <span class='id identifier rubyid_csv_opts'>csv_opts</span> <span class='op'>=</span> <span class='lbrace'>{</span> <span class='comment'># default options, new opts will override old ones
</span>    <span class='symbol'>:feature_name_row</span> <span class='op'>=&gt;</span> <span class='int'>1</span><span class='comma'>,</span>    <span class='comment'># first row contains feature names
</span>    <span class='symbol'>:feature_type_row</span> <span class='op'>=&gt;</span> <span class='int'>2</span><span class='comma'>,</span>    <span class='comment'># second row contains feature types
</span>    <span class='symbol'>:class_label_column</span> <span class='op'>=&gt;</span> <span class='int'>0</span><span class='comma'>,</span>  <span class='comment'># last column contains class labels
</span>    <span class='symbol'>:feature_name2type</span> <span class='op'>=&gt;</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>   <span class='comment'># user-supplied feature name-type pairs
</span>  <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_merge'>merge</span><span class='lparen'>(</span><span class='id identifier rubyid_csv_opts'>csv_opts</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_csv_opts'>csv_opts</span> <span class='kw'>and</span> <span class='id identifier rubyid_csv_opts'>csv_opts</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span> <span class='op'>==</span> <span class='const'>Hash</span>
  
  <span class='id identifier rubyid_feature_name_row'>feature_name_row</span> <span class='op'>=</span> <span class='id identifier rubyid_csv_opts'>csv_opts</span><span class='lbracket'>[</span><span class='symbol'>:feature_name_row</span><span class='rbracket'>]</span>
  <span class='id identifier rubyid_feature_type_row'>feature_type_row</span> <span class='op'>=</span> <span class='id identifier rubyid_csv_opts'>csv_opts</span><span class='lbracket'>[</span><span class='symbol'>:feature_type_row</span><span class='rbracket'>]</span>
  <span class='id identifier rubyid_class_label_column'>class_label_column</span> <span class='op'>=</span> <span class='id identifier rubyid_csv_opts'>csv_opts</span><span class='lbracket'>[</span><span class='symbol'>:class_label_column</span><span class='rbracket'>]</span>
  <span class='id identifier rubyid_feature_name2type'>feature_name2type</span> <span class='op'>=</span> <span class='id identifier rubyid_csv_opts'>csv_opts</span><span class='lbracket'>[</span><span class='symbol'>:feature_name2type</span><span class='rbracket'>]</span>
  
  <span class='comment'># user-supplied feature name-type pairs, this is useful 
</span>  <span class='comment'># when file contains no specific rows for feture names and types
</span>  <span class='kw'>if</span> <span class='id identifier rubyid_feature_name2type'>feature_name2type</span> <span class='kw'>and</span> <span class='kw'>not</span> <span class='id identifier rubyid_feature_name2type'>feature_name2type</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span>
    <span class='id identifier rubyid_features'>features</span> <span class='op'>=</span> <span class='id identifier rubyid_feature_name2type'>feature_name2type</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span><span class='period'>.</span><span class='id identifier rubyid_collect'>collect</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_n'>n</span><span class='op'>|</span> <span class='id identifier rubyid_n'>n</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span> <span class='rbrace'>}</span>
    <span class='id identifier rubyid_types'>types</span> <span class='op'>=</span> <span class='id identifier rubyid_feature_name2type'>feature_name2type</span><span class='period'>.</span><span class='id identifier rubyid_values'>values</span><span class='period'>.</span><span class='id identifier rubyid_collect'>collect</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_t'>t</span><span class='op'>|</span> <span class='id identifier rubyid_t'>t</span><span class='period'>.</span><span class='id identifier rubyid_downcase'>downcase</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span> <span class='rbrace'>}</span>
    <span class='comment'># disable name and type rows
</span>    <span class='id identifier rubyid_feature_name_row'>feature_name_row</span><span class='comma'>,</span> <span class='id identifier rubyid_feature_type_row'>feature_type_row</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='kw'>nil</span>
  <span class='kw'>end</span>
  
  <span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
  
  <span class='id identifier rubyid_ifs'>ifs</span><span class='period'>.</span><span class='id identifier rubyid_each_line'>each_line</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_ln'>ln</span><span class='op'>|</span>
    <span class='kw'>next</span> <span class='kw'>if</span> <span class='id identifier rubyid_ln'>ln</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
    
    <span class='id identifier rubyid_cells'>cells</span> <span class='op'>=</span> <span class='id identifier rubyid_ln'>ln</span><span class='period'>.</span><span class='id identifier rubyid_chomp'>chomp</span><span class='period'>.</span><span class='id identifier rubyid_split'>split</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>,</span><span class='regexp_end'>/</span></span><span class='rparen'>)</span>
    
    <span class='comment'># feature names
</span>    <span class='kw'>if</span> <span class='id identifier rubyid_ifs'>ifs</span><span class='period'>.</span><span class='id identifier rubyid_lineno'>lineno</span> <span class='op'>==</span> <span class='id identifier rubyid_feature_name_row'>feature_name_row</span>
      <span class='id identifier rubyid_class_name'>class_name</span> <span class='op'>=</span> <span class='id identifier rubyid_cells'>cells</span><span class='period'>.</span><span class='id identifier rubyid_delete_at'>delete_at</span><span class='lparen'>(</span><span class='id identifier rubyid_class_label_column'>class_label_column</span><span class='op'>-</span><span class='int'>1</span><span class='rparen'>)</span> <span class='comment'># simply useless
</span>      <span class='id identifier rubyid_features'>features</span> <span class='op'>=</span> <span class='id identifier rubyid_cells'>cells</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
    <span class='comment'># feature types
</span>    <span class='kw'>elsif</span> <span class='id identifier rubyid_ifs'>ifs</span><span class='period'>.</span><span class='id identifier rubyid_lineno'>lineno</span> <span class='op'>==</span> <span class='id identifier rubyid_feature_type_row'>feature_type_row</span>
      <span class='id identifier rubyid_class_type'>class_type</span> <span class='op'>=</span> <span class='id identifier rubyid_cells'>cells</span><span class='period'>.</span><span class='id identifier rubyid_delete_at'>delete_at</span><span class='lparen'>(</span><span class='id identifier rubyid_class_label_column'>class_label_column</span><span class='op'>-</span><span class='int'>1</span><span class='rparen'>)</span> <span class='comment'># simply useless
</span>      <span class='comment'># store feature type as lower-case symbol
</span>      <span class='id identifier rubyid_types'>types</span> <span class='op'>=</span> <span class='id identifier rubyid_cells'>cells</span><span class='period'>.</span><span class='id identifier rubyid_collect'>collect</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_t'>t</span><span class='op'>|</span> <span class='id identifier rubyid_t'>t</span><span class='period'>.</span><span class='id identifier rubyid_downcase'>downcase</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span> <span class='rbrace'>}</span>
    <span class='kw'>else</span> <span class='comment'># data rows
</span>      <span class='kw'>if</span> <span class='id identifier rubyid_class_label_column'>class_label_column</span> <span class='op'>&lt;=</span> <span class='id identifier rubyid_cells'>cells</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span> <span class='kw'>and</span> 
         <span class='id identifier rubyid_features'>features</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='op'>+</span><span class='int'>1</span> <span class='op'>==</span> <span class='id identifier rubyid_cells'>cells</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span> <span class='kw'>and</span> 
         <span class='id identifier rubyid_types'>types</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='op'>+</span><span class='int'>1</span> <span class='op'>==</span> <span class='id identifier rubyid_cells'>cells</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span>
        <span class='id identifier rubyid_class_label'>class_label</span> <span class='op'>=</span> <span class='id identifier rubyid_cells'>cells</span><span class='period'>.</span><span class='id identifier rubyid_delete_at'>delete_at</span><span class='lparen'>(</span><span class='id identifier rubyid_class_label_column'>class_label_column</span><span class='op'>-</span><span class='int'>1</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
        <span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='id identifier rubyid_class_label'>class_label</span><span class='rbracket'>]</span> <span class='op'>||=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
        <span class='comment'># feature values
</span>        <span class='id identifier rubyid_fvs'>fvs</span> <span class='op'>=</span> <span class='id identifier rubyid_cells'>cells</span>
      <span class='kw'>else</span>
        <span class='id identifier rubyid_abort'>abort</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>[</span><span class='embexpr_beg'>#{</span><span class='kw'>__FILE__</span><span class='rbrace'>}</span><span class='tstring_content'>@</span><span class='embexpr_beg'>#{</span><span class='kw'>__LINE__</span><span class='rbrace'>}</span><span class='tstring_content'>]: \n</span><span class='tstring_end'>&quot;</span></span><span class='op'>+</span>
              <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>  invalid csv format!</span><span class='tstring_end'>&quot;</span></span>
      <span class='kw'>end</span>              
      
      <span class='id identifier rubyid_fs'>fs</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
      <span class='id identifier rubyid_fvs'>fvs</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_v'>v</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
        <span class='kw'>next</span> <span class='kw'>if</span> <span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span> <span class='comment'># missing value
</span>        <span class='id identifier rubyid_type'>type</span> <span class='op'>=</span> <span class='id identifier rubyid_types'>types</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span>
        <span class='kw'>if</span> <span class='id identifier rubyid_type'>type</span> <span class='op'>==</span> <span class='symbol'>:integer</span>
          <span class='id identifier rubyid_v'>v</span> <span class='op'>=</span> <span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_to_i'>to_i</span>
        <span class='kw'>elsif</span> <span class='lbracket'>[</span><span class='symbol'>:real</span><span class='comma'>,</span> <span class='symbol'>:numeric</span><span class='comma'>,</span> <span class='symbol'>:continuous</span><span class='comma'>,</span> <span class='symbol'>:float</span><span class='comma'>,</span> <span class='symbol'>:double</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_include?'>include?</span> <span class='id identifier rubyid_type'>type</span>
          <span class='id identifier rubyid_v'>v</span> <span class='op'>=</span> <span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
        <span class='kw'>elsif</span> <span class='lbracket'>[</span><span class='symbol'>:string</span><span class='comma'>,</span> <span class='symbol'>:nominal</span><span class='comma'>,</span> <span class='symbol'>:categorical</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_include?'>include?</span> <span class='id identifier rubyid_type'>type</span>
          <span class='comment'>#
</span>        <span class='kw'>else</span>
          <span class='id identifier rubyid_abort'>abort</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>[</span><span class='embexpr_beg'>#{</span><span class='kw'>__FILE__</span><span class='rbrace'>}</span><span class='tstring_content'>@</span><span class='embexpr_beg'>#{</span><span class='kw'>__LINE__</span><span class='rbrace'>}</span><span class='tstring_content'>]: \n</span><span class='tstring_end'>&quot;</span></span><span class='op'>+</span>
                <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>  invalid feature type '</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_type'>type</span><span class='rbrace'>}</span><span class='tstring_content'>', must be one of the following: \n</span><span class='tstring_end'>&quot;</span></span><span class='op'>+</span>
                <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>  integer, real, numeric, float, double, continuous, categorical, string, nominal</span><span class='tstring_end'>&quot;</span></span>
        <span class='kw'>end</span>
        
        <span class='id identifier rubyid_fs'>fs</span><span class='lbracket'>[</span><span class='id identifier rubyid_features'>features</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_v'>v</span>
      <span class='kw'>end</span>
      
      <span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='id identifier rubyid_class_label'>class_label</span><span class='rbracket'>]</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_fs'>fs</span>
    <span class='kw'>end</span>
  <span class='kw'>end</span>
  
  <span class='comment'># close file
</span>  <span class='id identifier rubyid_ifs'>ifs</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_ifs'>ifs</span> <span class='op'>==</span> <span class='gvar'>$stdin</span>
  
  <span class='id identifier rubyid_set_data'>set_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_set_features'>set_features</span><span class='lparen'>(</span><span class='id identifier rubyid_features'>features</span><span class='rparen'>)</span>
  
  <span class='comment'># feature name-type pairs
</span>  <span class='id identifier rubyid_features'>features</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
    <span class='id identifier rubyid_set_feature_type'>set_feature_type</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='comma'>,</span> <span class='id identifier rubyid_types'>types</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span><span class='rparen'>)</span>
  <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="data_from_libsvm-instance_method">
  
    - <strong>data_from_libsvm</strong>(fname = :stdin) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>read from libsvm</p>

<p>file has the following format<br>
+1  2:1 4:1 ...<br>
-1  3:1 4:1 ...<br>
....</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>fname</span>
      
      
        <span class='type'>(<tt>Symbol|<span class='object_link'><a href="String.html" title="String (class)">String</a></span>|StringIO</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>:stdin</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>data source to read from<br>
:stdin  # read from standard input instead of file</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/fileio.rb', line 87</span>

<span class='kw'>def</span> <span class='id identifier rubyid_data_from_libsvm'>data_from_libsvm</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='op'>=</span><span class='symbol'>:stdin</span><span class='rparen'>)</span>    
  <span class='id identifier rubyid_ifs'>ifs</span> <span class='op'>=</span> <span class='id identifier rubyid_get_ifs'>get_ifs</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='rparen'>)</span>
  
  <span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
  
  <span class='id identifier rubyid_ifs'>ifs</span><span class='period'>.</span><span class='id identifier rubyid_each_line'>each_line</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_ln'>ln</span><span class='op'>|</span>
    <span class='id identifier rubyid_label'>label</span><span class='comma'>,</span> <span class='op'>*</span><span class='id identifier rubyid_features'>features</span> <span class='op'>=</span> <span class='id identifier rubyid_ln'>ln</span><span class='period'>.</span><span class='id identifier rubyid_chomp'>chomp</span><span class='period'>.</span><span class='id identifier rubyid_split'>split</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\s+</span><span class='regexp_end'>/</span></span><span class='rparen'>)</span>
    <span class='id identifier rubyid_label'>label</span> <span class='op'>=</span> <span class='id identifier rubyid_label'>label</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
    <span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='id identifier rubyid_label'>label</span><span class='rbracket'>]</span> <span class='op'>||=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
    
    <span class='id identifier rubyid_feats'>feats</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
    <span class='id identifier rubyid_features'>features</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_fv'>fv</span><span class='op'>|</span>
      <span class='id identifier rubyid_f'>f</span><span class='comma'>,</span> <span class='id identifier rubyid_v'>v</span> <span class='op'>=</span> <span class='id identifier rubyid_fv'>fv</span><span class='period'>.</span><span class='id identifier rubyid_split'>split</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>:</span><span class='regexp_end'>/</span></span><span class='rparen'>)</span>
      <span class='id identifier rubyid_feats'>feats</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
    <span class='kw'>end</span>
    
    <span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='id identifier rubyid_label'>label</span><span class='rbracket'>]</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_feats'>feats</span>
  <span class='kw'>end</span>
  
  <span class='comment'># close file
</span>  <span class='id identifier rubyid_ifs'>ifs</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_ifs'>ifs</span> <span class='op'>==</span> <span class='gvar'>$stdin</span>
  
  <span class='id identifier rubyid_set_data'>set_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
  
  <span class='comment'># feature name-type pairs
</span>  <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
    <span class='id identifier rubyid_set_feature_type'>set_feature_type</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='comma'>,</span> <span class='symbol'>:numeric</span><span class='rparen'>)</span>
  <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="data_from_random-instance_method">
  
    - <strong>data_from_random</strong>(nsample = 100, nclass = 2, nfeature = 10, ncategory = 2, allow_mv = true) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>read from random data (read only, for test purpose)</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>nsample</span>
      
      
        <span class='type'>(<tt>Integer</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>100</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>number of total samples</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>nclass</span>
      
      
        <span class='type'>(<tt>Integer</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>2</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>number of classes</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>nfeature</span>
      
      
        <span class='type'>(<tt>Integer</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>10</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>number of features</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>ncategory</span>
      
      
        <span class='type'>(<tt>Integer</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>2</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>number of categories for each feature<br>
1      # binary feature with only on bit<br>
&gt;1     # discrete feature with multiple values<br>
other  # continuous feature with vaule in the range of [0, 1)</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>allow_mv</span>
      
      
        <span class='type'>(<tt>true</tt>, <tt>false</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>true</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>whether missing value of feature is alowed or not</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/fileio.rb', line 40</span>

<span class='kw'>def</span> <span class='id identifier rubyid_data_from_random'>data_from_random</span><span class='lparen'>(</span><span class='id identifier rubyid_nsample'>nsample</span><span class='op'>=</span><span class='int'>100</span><span class='comma'>,</span> <span class='id identifier rubyid_nclass'>nclass</span><span class='op'>=</span><span class='int'>2</span><span class='comma'>,</span> <span class='id identifier rubyid_nfeature'>nfeature</span><span class='op'>=</span><span class='int'>10</span><span class='comma'>,</span> <span class='id identifier rubyid_ncategory'>ncategory</span><span class='op'>=</span><span class='int'>2</span><span class='comma'>,</span> <span class='id identifier rubyid_allow_mv'>allow_mv</span><span class='op'>=</span><span class='kw'>true</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>

  <span class='id identifier rubyid_nsample'>nsample</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='kw'>do</span>
    <span class='id identifier rubyid_k'>k</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>c</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_rand'>rand</span><span class='lparen'>(</span><span class='id identifier rubyid_nclass'>nclass</span><span class='rparen'>)</span><span class='op'>+</span><span class='int'>1</span><span class='rbrace'>}</span><span class='tstring_end'>&quot;</span></span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
    
    <span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span> <span class='op'>||=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
    
    <span class='id identifier rubyid_feats'>feats</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
    <span class='id identifier rubyid_fs'>fs</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='int'>1</span><span class='op'>..</span><span class='id identifier rubyid_nfeature'>nfeature</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_a'>to_a</span>
    
    <span class='kw'>if</span> <span class='id identifier rubyid_allow_mv'>allow_mv</span>
      <span class='lparen'>(</span><span class='id identifier rubyid_rand'>rand</span><span class='lparen'>(</span><span class='id identifier rubyid_nfeature'>nfeature</span><span class='rparen'>)</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='kw'>do</span>
        <span class='id identifier rubyid_v'>v</span> <span class='op'>=</span> <span class='id identifier rubyid_fs'>fs</span><span class='lbracket'>[</span><span class='id identifier rubyid_rand'>rand</span><span class='lparen'>(</span><span class='id identifier rubyid_fs'>fs</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='rparen'>)</span><span class='rbracket'>]</span>
        <span class='id identifier rubyid_fs'>fs</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span>
      <span class='kw'>end</span>
    <span class='kw'>end</span>
    
    <span class='id identifier rubyid_fs'>fs</span><span class='period'>.</span><span class='id identifier rubyid_sort'>sort</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
      <span class='id identifier rubyid_f'>f</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>f</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_i'>i</span><span class='rbrace'>}</span><span class='tstring_end'>&quot;</span></span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
      <span class='kw'>if</span> <span class='id identifier rubyid_ncategory'>ncategory</span> <span class='op'>==</span> <span class='int'>1</span>
        <span class='id identifier rubyid_feats'>feats</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='int'>1</span>
      <span class='kw'>elsif</span> <span class='id identifier rubyid_ncategory'>ncategory</span> <span class='op'>&gt;</span> <span class='int'>1</span>
        <span class='id identifier rubyid_feats'>feats</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_rand'>rand</span><span class='lparen'>(</span><span class='id identifier rubyid_ncategory'>ncategory</span><span class='rparen'>)</span><span class='op'>+</span><span class='int'>1</span>
      <span class='kw'>else</span>
        <span class='id identifier rubyid_feats'>feats</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_rand'>rand</span><span class='period'>.</span><span class='id identifier rubyid_round'>round</span><span class='lparen'>(</span><span class='int'>3</span><span class='rparen'>)</span> <span class='comment'># round to 3-digit precision
</span>      <span class='kw'>end</span>
    <span class='kw'>end</span>
    
    <span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_feats'>feats</span>
  <span class='kw'>end</span>

  <span class='id identifier rubyid_set_data'>set_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="data_from_url-instance_method">
  
    - <strong>data_from_url</strong>(url, format, *args) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>read data from url</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>url</span>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="String.html" title="String (class)">String</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>url of on-line dataset</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>format</span>
      
      
        <span class='type'>(<tt>Symbol</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>allowed formats are:<br>
:libsvm  # LibSVM file<br>
:csv     # csv file<br>
:weka    # Weka ARFF file</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>args</span>
      
      
        <span class='type'>(<tt>Any</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>arguments associated with format</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/fileio.rb', line 504</span>

<span class='kw'>def</span> <span class='id identifier rubyid_data_from_url'>data_from_url</span><span class='lparen'>(</span><span class='id identifier rubyid_url'>url</span><span class='comma'>,</span> <span class='id identifier rubyid_format'>format</span><span class='comma'>,</span> <span class='op'>*</span><span class='id identifier rubyid_args'>args</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_format'>format</span> <span class='op'>=</span> <span class='id identifier rubyid_format'>format</span><span class='period'>.</span><span class='id identifier rubyid_downcase'>downcase</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
  
  <span class='kw'>if</span> <span class='kw'>not</span> <span class='lbracket'>[</span><span class='symbol'>:libsvm</span><span class='comma'>,</span> <span class='symbol'>:csv</span><span class='comma'>,</span> <span class='symbol'>:weka</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_include?'>include?</span> <span class='id identifier rubyid_format'>format</span>
    <span class='id identifier rubyid_abort'>abort</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>[</span><span class='embexpr_beg'>#{</span><span class='kw'>__FILE__</span><span class='rbrace'>}</span><span class='tstring_content'>@</span><span class='embexpr_beg'>#{</span><span class='kw'>__LINE__</span><span class='rbrace'>}</span><span class='tstring_content'>]: \n</span><span class='tstring_end'>&quot;</span></span><span class='op'>+</span>
          <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>  only CSV, LibSVM and Weka file formats are supported!</span><span class='tstring_end'>&quot;</span></span>
  <span class='kw'>end</span>
  
  <span class='id identifier rubyid_uri'>uri</span> <span class='op'>=</span> <span class='const'>URI</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='const'>URI</span><span class='period'>.</span><span class='id identifier rubyid_encode'>encode</span><span class='lparen'>(</span><span class='id identifier rubyid_url'>url</span><span class='rparen'>)</span><span class='rparen'>)</span>
  
  <span class='id identifier rubyid_data_src'>data_src</span> <span class='op'>=</span> <span class='const'>StringIO</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_uri'>uri</span><span class='period'>.</span><span class='id identifier rubyid_read'>read</span><span class='rparen'>)</span>
  
  <span class='kw'>if</span> <span class='id identifier rubyid_format'>format</span> <span class='op'>==</span> <span class='symbol'>:csv</span>
    <span class='id identifier rubyid_data_from_csv'>data_from_csv</span><span class='lparen'>(</span><span class='id identifier rubyid_data_src'>data_src</span><span class='comma'>,</span> <span class='op'>*</span><span class='id identifier rubyid_args'>args</span><span class='rparen'>)</span>
  <span class='kw'>elsif</span> <span class='id identifier rubyid_format'>format</span> <span class='op'>==</span> <span class='symbol'>:libsvm</span>
    <span class='id identifier rubyid_data_from_libsvm'>data_from_libsvm</span><span class='lparen'>(</span><span class='id identifier rubyid_data_src'>data_src</span><span class='rparen'>)</span>
  <span class='kw'>else</span> <span class='comment'># weka
</span>    <span class='id identifier rubyid_data_from_weka'>data_from_weka</span><span class='lparen'>(</span><span class='id identifier rubyid_data_src'>data_src</span><span class='comma'>,</span> <span class='op'>*</span><span class='id identifier rubyid_args'>args</span><span class='rparen'>)</span>
  <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="data_from_weka-instance_method">
  
    - <strong>data_from_weka</strong>(fname = :stdin, quote_char = '&quot;') 
  

  
</p><div class="docstring">
  <div class="discussion">
    
  <div class="note notetag">
    <strong>Note:</strong> 
    <div class='inline'><p>it&#39;s ok if string containes spaces quoted by quote_char</p>
</div>
  </div>

<p>read from WEKA ARFF file</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>fname</span>
      
      
        <span class='type'>(<tt>Symbol|<span class='object_link'><a href="String.html" title="String (class)">String</a></span>|StringIO</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>:stdin</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>data source to read from<br>
:stdin  # read from standard input</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/fileio.rb', line 320</span>

<span class='kw'>def</span> <span class='id identifier rubyid_data_from_weka'>data_from_weka</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='op'>=</span><span class='symbol'>:stdin</span><span class='comma'>,</span> <span class='id identifier rubyid_quote_char'>quote_char</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>&quot;</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
  <span class='id identifier rubyid_ifs'>ifs</span> <span class='op'>=</span> <span class='id identifier rubyid_get_ifs'>get_ifs</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='rparen'>)</span>
  
  <span class='id identifier rubyid_relation'>relation</span><span class='comma'>,</span> <span class='id identifier rubyid_features'>features</span><span class='comma'>,</span> <span class='id identifier rubyid_classes'>classes</span><span class='comma'>,</span> <span class='id identifier rubyid_types'>types</span><span class='comma'>,</span> <span class='id identifier rubyid_comments'>comments</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
  <span class='id identifier rubyid_has_class'>has_class</span><span class='comma'>,</span> <span class='id identifier rubyid_has_data'>has_data</span> <span class='op'>=</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='kw'>false</span>
  
  <span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
  
  <span class='id identifier rubyid_ifs'>ifs</span><span class='period'>.</span><span class='id identifier rubyid_each_line'>each_line</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_ln'>ln</span><span class='op'>|</span>
    <span class='kw'>next</span> <span class='kw'>if</span> <span class='id identifier rubyid_ln'>ln</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='comment'># blank lines
</span>    
    <span class='id identifier rubyid_ln'>ln</span> <span class='op'>=</span> <span class='id identifier rubyid_ln'>ln</span><span class='period'>.</span><span class='id identifier rubyid_chomp'>chomp</span>
    
    <span class='comment'># comment line
</span>    <span class='kw'>if</span> <span class='id identifier rubyid_ln'>ln</span><span class='period'>.</span><span class='id identifier rubyid_comment?'>comment?</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>%</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
      <span class='id identifier rubyid_comments'>comments</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_ln'>ln</span>
    <span class='comment'># relation
</span>    <span class='kw'>elsif</span> <span class='id identifier rubyid_ln'>ln</span> <span class='op'>=~</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>^@RELATION</span><span class='regexp_end'>/i</span></span>
      <span class='id identifier rubyid_tmp'>tmp</span><span class='comma'>,</span> <span class='id identifier rubyid_relation'>relation</span> <span class='op'>=</span> <span class='id identifier rubyid_ln'>ln</span><span class='period'>.</span><span class='id identifier rubyid_split_me'>split_me</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\s+</span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='id identifier rubyid_quote_char'>quote_char</span><span class='rparen'>)</span>
    <span class='comment'># class attribute
</span>    <span class='kw'>elsif</span> <span class='id identifier rubyid_ln'>ln</span> <span class='op'>=~</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>^@ATTRIBUTE\s+class\s+{(.+)}</span><span class='regexp_end'>/i</span></span>
      <span class='id identifier rubyid_has_class'>has_class</span> <span class='op'>=</span> <span class='kw'>true</span>
      <span class='id identifier rubyid_classes'>classes</span> <span class='op'>=</span> <span class='backref'>$1</span><span class='period'>.</span><span class='id identifier rubyid_strip'>strip</span><span class='period'>.</span><span class='id identifier rubyid_split_me'>split_me</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>,\s*</span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='id identifier rubyid_quote_char'>quote_char</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
      <span class='id identifier rubyid_classes'>classes</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='op'>|</span> <span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span> <span class='rbrace'>}</span>
    <span class='comment'># feature attribute (nominal)
</span>    <span class='kw'>elsif</span> <span class='id identifier rubyid_ln'>ln</span> <span class='op'>=~</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>^@ATTRIBUTE\s+(\S+)\s+{(.+)}</span><span class='regexp_end'>/i</span></span>
      <span class='id identifier rubyid_f'>f</span> <span class='op'>=</span> <span class='backref'>$1</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
      <span class='id identifier rubyid_features'>features</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_f'>f</span>
      <span class='comment'>#$2.split_me(/,\s*/, quote_char) # feature nominal values
</span>      <span class='id identifier rubyid_types'>types</span> <span class='op'>&lt;&lt;</span> <span class='symbol'>:nominal</span>
    <span class='comment'># feature attribute (categorical, integer, real, continuous, numeric, float, double, string, date)
</span>    <span class='kw'>elsif</span> <span class='id identifier rubyid_ln'>ln</span> <span class='op'>=~</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>^@ATTRIBUTE</span><span class='regexp_end'>/i</span></span>
      <span class='id identifier rubyid_tmp'>tmp</span><span class='comma'>,</span> <span class='id identifier rubyid_v1'>v1</span><span class='comma'>,</span> <span class='id identifier rubyid_v2'>v2</span> <span class='op'>=</span> <span class='id identifier rubyid_ln'>ln</span><span class='period'>.</span><span class='id identifier rubyid_split_me'>split_me</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\s+</span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='id identifier rubyid_quote_char'>quote_char</span><span class='rparen'>)</span>
      <span class='id identifier rubyid_f'>f</span> <span class='op'>=</span> <span class='id identifier rubyid_v1'>v1</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
      <span class='id identifier rubyid_features'>features</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_f'>f</span>
      <span class='comment'># store feture type as lower-case symbol
</span>      <span class='id identifier rubyid_types'>types</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_v2'>v2</span><span class='period'>.</span><span class='id identifier rubyid_downcase'>downcase</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
    <span class='comment'># data header
</span>    <span class='kw'>elsif</span> <span class='id identifier rubyid_ln'>ln</span> <span class='op'>=~</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>^@DATA</span><span class='regexp_end'>/i</span></span>
      <span class='id identifier rubyid_has_data'>has_data</span> <span class='op'>=</span> <span class='kw'>true</span>
    <span class='comment'># data
</span>    <span class='kw'>elsif</span> <span class='id identifier rubyid_has_data'>has_data</span> <span class='kw'>and</span> <span class='id identifier rubyid_has_class'>has_class</span>
      <span class='comment'># read data section
</span>      <span class='kw'>if</span> <span class='id identifier rubyid_ln'>ln</span> <span class='op'>=~</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>^{(.+)}$</span><span class='regexp_end'>/</span></span> <span class='comment'># sparse ARFF
</span>        <span class='id identifier rubyid_feats'>feats</span> <span class='op'>=</span> <span class='backref'>$1</span><span class='period'>.</span><span class='id identifier rubyid_split_me'>split_me</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>,\s*</span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='id identifier rubyid_quote_char'>quote_char</span><span class='rparen'>)</span>
        <span class='id identifier rubyid_label'>label</span> <span class='op'>=</span> <span class='id identifier rubyid_feats'>feats</span><span class='period'>.</span><span class='id identifier rubyid_pop'>pop</span><span class='period'>.</span><span class='id identifier rubyid_split_me'>split_me</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\s+</span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='id identifier rubyid_quote_char'>quote_char</span><span class='rparen'>)</span><span class='lbracket'>[</span><span class='int'>1</span><span class='rbracket'>]</span>
        <span class='id identifier rubyid_label'>label</span> <span class='op'>=</span> <span class='id identifier rubyid_label'>label</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
        
        <span class='id identifier rubyid_fs'>fs</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
        <span class='comment'># indices of feature with zero value
</span>        <span class='id identifier rubyid_zero_fi'>zero_fi</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='int'>0</span><span class='op'>...</span><span class='id identifier rubyid_features'>features</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_a'>to_a</span>
        
        <span class='id identifier rubyid_feats'>feats</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_fi_fv'>fi_fv</span><span class='op'>|</span>
          <span class='id identifier rubyid_fi'>fi</span><span class='comma'>,</span> <span class='id identifier rubyid_fv'>fv</span> <span class='op'>=</span> <span class='id identifier rubyid_fi_fv'>fi_fv</span><span class='period'>.</span><span class='id identifier rubyid_split_me'>split_me</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\s+</span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='id identifier rubyid_quote_char'>quote_char</span><span class='rparen'>)</span>
          <span class='id identifier rubyid_fi'>fi</span> <span class='op'>=</span> <span class='id identifier rubyid_fi'>fi</span><span class='period'>.</span><span class='id identifier rubyid_to_i'>to_i</span>             
          <span class='id identifier rubyid_add_feature_weka'>add_feature_weka</span><span class='lparen'>(</span><span class='id identifier rubyid_fs'>fs</span><span class='comma'>,</span> <span class='id identifier rubyid_features'>features</span><span class='lbracket'>[</span><span class='id identifier rubyid_fi'>fi</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_fv'>fv</span><span class='comma'>,</span> <span class='id identifier rubyid_types'>types</span><span class='lbracket'>[</span><span class='id identifier rubyid_fi'>fi</span><span class='rbracket'>]</span><span class='rparen'>)</span>
          <span class='id identifier rubyid_zero_fi'>zero_fi</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_fi'>fi</span><span class='rparen'>)</span>
        <span class='kw'>end</span>
        
        <span class='comment'># feature with zero value
</span>        <span class='id identifier rubyid_zero_fi'>zero_fi</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_zi'>zi</span><span class='op'>|</span>
          <span class='id identifier rubyid_add_feature_weka'>add_feature_weka</span><span class='lparen'>(</span><span class='id identifier rubyid_fs'>fs</span><span class='comma'>,</span> <span class='id identifier rubyid_features'>features</span><span class='lbracket'>[</span><span class='id identifier rubyid_zi'>zi</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='int'>0</span><span class='comma'>,</span> <span class='id identifier rubyid_types'>types</span><span class='lbracket'>[</span><span class='id identifier rubyid_zi'>zi</span><span class='rbracket'>]</span><span class='rparen'>)</span>
        <span class='kw'>end</span>
        
        <span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='id identifier rubyid_label'>label</span><span class='rbracket'>]</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_fs'>fs</span>
      <span class='kw'>else</span> <span class='comment'># regular ARFF
</span>        <span class='id identifier rubyid_feats'>feats</span> <span class='op'>=</span> <span class='id identifier rubyid_ln'>ln</span><span class='period'>.</span><span class='id identifier rubyid_split_me'>split_me</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>,\s*</span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='id identifier rubyid_quote_char'>quote_char</span><span class='rparen'>)</span>
        <span class='id identifier rubyid_label'>label</span> <span class='op'>=</span> <span class='id identifier rubyid_feats'>feats</span><span class='period'>.</span><span class='id identifier rubyid_pop'>pop</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>          
        
        <span class='id identifier rubyid_fs'>fs</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
        <span class='id identifier rubyid_feats'>feats</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_fv'>fv</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
          <span class='id identifier rubyid_add_feature_weka'>add_feature_weka</span><span class='lparen'>(</span><span class='id identifier rubyid_fs'>fs</span><span class='comma'>,</span> <span class='id identifier rubyid_features'>features</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_fv'>fv</span><span class='comma'>,</span> <span class='id identifier rubyid_types'>types</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span><span class='rparen'>)</span>
        <span class='kw'>end</span>
        <span class='id identifier rubyid_data'>data</span><span class='lbracket'>[</span><span class='id identifier rubyid_label'>label</span><span class='rbracket'>]</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_fs'>fs</span> <span class='kw'>if</span> <span class='id identifier rubyid_label'>label</span>
      <span class='kw'>end</span>
    <span class='kw'>else</span>
      <span class='kw'>next</span>
    <span class='kw'>end</span>
  <span class='kw'>end</span>
  
  <span class='comment'># close file
</span>  <span class='id identifier rubyid_ifs'>ifs</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_ifs'>ifs</span> <span class='op'>==</span> <span class='gvar'>$stdin</span>
  
  <span class='id identifier rubyid_set_data'>set_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_set_classes'>set_classes</span><span class='lparen'>(</span><span class='id identifier rubyid_classes'>classes</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_set_features'>set_features</span><span class='lparen'>(</span><span class='id identifier rubyid_features'>features</span><span class='rparen'>)</span>
  <span class='comment'># feature name-type pairs
</span>  <span class='id identifier rubyid_features'>features</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
    <span class='id identifier rubyid_set_feature_type'>set_feature_type</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='comma'>,</span> <span class='id identifier rubyid_types'>types</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span><span class='rparen'>)</span>
  <span class='kw'>end</span>
  
  <span class='id identifier rubyid_set_opt'>set_opt</span><span class='lparen'>(</span><span class='symbol'>:relation</span><span class='comma'>,</span> <span class='id identifier rubyid_relation'>relation</span><span class='rparen'>)</span>    
  <span class='id identifier rubyid_set_opt'>set_opt</span><span class='lparen'>(</span><span class='symbol'>:comments</span><span class='comma'>,</span> <span class='id identifier rubyid_comments'>comments</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_comments'>comments</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="data_to_csv-instance_method">
  
    - <strong>data_to_csv</strong>(fname = :stdout) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>write to csv</p>

<p>ouput CSV file has the same format as the default input for data_from_csv()</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>fname</span>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="String.html" title="String (class)">String</a></span></tt>)</span>
      
      
        <em class="default">(defaults to: <tt>:stdout</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>file to write<br>
:stdout  # write to standard ouput</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/fileio.rb', line 287</span>

<span class='kw'>def</span> <span class='id identifier rubyid_data_to_csv'>data_to_csv</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='op'>=</span><span class='symbol'>:stdout</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_ofs'>ofs</span> <span class='op'>=</span> <span class='id identifier rubyid_get_ofs'>get_ofs</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='rparen'>)</span>
  
  <span class='comment'># feature names 
</span>  <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_get_features'>get_features</span><span class='period'>.</span><span class='id identifier rubyid_join'>join</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
  <span class='comment'># feature types
</span>  <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_get_features'>get_features</span><span class='period'>.</span><span class='id identifier rubyid_collect'>collect</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span> 
    <span class='id identifier rubyid_get_feature_type'>get_feature_type</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span> <span class='op'>||</span> <span class='symbol'>:string</span>
  <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_join'>join</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
  
  <span class='id identifier rubyid_each_sample'>each_sample</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='comma'>,</span> <span class='id identifier rubyid_s'>s</span><span class='op'>|</span>
    <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
      <span class='kw'>if</span> <span class='id identifier rubyid_s'>s</span><span class='period'>.</span><span class='id identifier rubyid_has_key?'>has_key?</span> <span class='id identifier rubyid_f'>f</span>
        <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_s'>s</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='rbrace'>}</span><span class='tstring_content'>,</span><span class='tstring_end'>&quot;</span></span>
      <span class='kw'>else</span>
        <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>,</span><span class='tstring_end'>&quot;</span></span>
      <span class='kw'>end</span>
    <span class='kw'>end</span>
    <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_k'>k</span><span class='rbrace'>}</span><span class='tstring_end'>&quot;</span></span>
  <span class='kw'>end</span>
  
  <span class='comment'># close file
</span>  <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_ofs'>ofs</span> <span class='op'>==</span> <span class='gvar'>$stdout</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="data_to_libsvm-instance_method">
  
    - <strong>data_to_libsvm</strong>(fname = :stdout) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>write to libsvm</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>fname</span>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="String.html" title="String (class)">String</a></span></tt>)</span>
      
      
        <em class="default">(defaults to: <tt>:stdout</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>file to write<br>
:stdout  # write to standard ouput</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/fileio.rb', line 124</span>

<span class='kw'>def</span> <span class='id identifier rubyid_data_to_libsvm'>data_to_libsvm</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='op'>=</span><span class='symbol'>:stdout</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_ofs'>ofs</span> <span class='op'>=</span> <span class='id identifier rubyid_get_ofs'>get_ofs</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='rparen'>)</span>
  
  <span class='comment'># convert class label to integer type
</span>  <span class='id identifier rubyid_k2idx'>k2idx</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
  <span class='id identifier rubyid_get_classes'>get_classes</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
    <span class='id identifier rubyid_k2idx'>k2idx</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>1</span>
  <span class='kw'>end</span>
  
  <span class='comment'># convert feature to integer type
</span>  <span class='id identifier rubyid_f2idx'>f2idx</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
  <span class='id identifier rubyid_get_features'>get_features</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
    <span class='id identifier rubyid_f2idx'>f2idx</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_i'>i</span><span class='op'>+</span><span class='int'>1</span>
  <span class='kw'>end</span>
  
  <span class='id identifier rubyid_each_sample'>each_sample</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='comma'>,</span> <span class='id identifier rubyid_s'>s</span><span class='op'>|</span>
    <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_k2idx'>k2idx</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='rbracket'>]</span><span class='rbrace'>}</span><span class='tstring_content'> </span><span class='tstring_end'>&quot;</span></span>
    <span class='id identifier rubyid_s'>s</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span><span class='period'>.</span><span class='id identifier rubyid_sort'>sort</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_x'>x</span><span class='comma'>,</span> <span class='id identifier rubyid_y'>y</span><span class='op'>|</span> <span class='id identifier rubyid_f2idx'>f2idx</span><span class='lbracket'>[</span><span class='id identifier rubyid_x'>x</span><span class='rbracket'>]</span> <span class='op'>&lt;=&gt;</span> <span class='id identifier rubyid_f2idx'>f2idx</span><span class='lbracket'>[</span><span class='id identifier rubyid_y'>y</span><span class='rbracket'>]</span> <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
      <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_s'>s</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span> <span class='const'>Numeric</span>
        <span class='id identifier rubyid_abort'>abort</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>[</span><span class='embexpr_beg'>#{</span><span class='kw'>__FILE__</span><span class='rbrace'>}</span><span class='tstring_content'>@</span><span class='embexpr_beg'>#{</span><span class='kw'>__LINE__</span><span class='rbrace'>}</span><span class='tstring_content'>]: \n</span><span class='tstring_end'>&quot;</span></span><span class='op'>+</span>
                <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>  LibSVM format only supports the following feature type: \n</span><span class='tstring_end'>&quot;</span></span><span class='op'>+</span>
                <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>  integer, real, numeric, float, double, continuous</span><span class='tstring_end'>&quot;</span></span>
      <span class='kw'>else</span>
        <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'> </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_f2idx'>f2idx</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='rbrace'>}</span><span class='tstring_content'>:</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_s'>s</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='rbrace'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_s'>s</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_zero?'>zero?</span> <span class='comment'># implicit mode
</span>      <span class='kw'>end</span>
    <span class='kw'>end</span>
    <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span>
  <span class='kw'>end</span>
  
  <span class='comment'># close file
</span>  <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_ofs'>ofs</span> <span class='op'>==</span> <span class='gvar'>$stdout</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="data_to_weka-instance_method">
  
    - <strong>data_to_weka</strong>(fname = :stdout, format = nil) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>write to WEKA ARFF file</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='name'>fname</span>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="String.html" title="String (class)">String</a></span></tt>)</span>
      
      
        <em class="default">(defaults to: <tt>:stdout</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>file to write<br>
:stdout  # write to standard ouput</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>format</span>
      
      
        <span class='type'>(<tt>Symbol</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>nil</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>sparse or regular ARFF<br>
:sparse  # sparse ARFF, otherwise regular ARFF</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/fselector/fileio.rb', line 424</span>

<span class='kw'>def</span> <span class='id identifier rubyid_data_to_weka'>data_to_weka</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='op'>=</span><span class='symbol'>:stdout</span><span class='comma'>,</span> <span class='id identifier rubyid_format'>format</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_ofs'>ofs</span> <span class='op'>=</span> <span class='id identifier rubyid_get_ofs'>get_ofs</span><span class='lparen'>(</span><span class='id identifier rubyid_fname'>fname</span><span class='rparen'>)</span>
  
  <span class='comment'># comments
</span>  <span class='id identifier rubyid_comments'>comments</span> <span class='op'>=</span> <span class='id identifier rubyid_get_opt'>get_opt</span><span class='lparen'>(</span><span class='symbol'>:comments</span><span class='rparen'>)</span>
  <span class='kw'>if</span> <span class='id identifier rubyid_comments'>comments</span>
    <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_comments'>comments</span><span class='period'>.</span><span class='id identifier rubyid_join'>join</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>\n</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
    <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span>
  <span class='kw'>end</span>         
  
  <span class='comment'># relation
</span>  <span class='id identifier rubyid_relation'>relation</span> <span class='op'>=</span> <span class='id identifier rubyid_get_opt'>get_opt</span><span class='lparen'>(</span><span class='symbol'>:relation</span><span class='rparen'>)</span>
  <span class='kw'>if</span> <span class='id identifier rubyid_relation'>relation</span>
    <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>@RELATION </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_relation'>relation</span><span class='rbrace'>}</span><span class='tstring_end'>&quot;</span></span>
  <span class='kw'>else</span>
    <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>@RELATION data_gen_by_FSelector</span><span class='tstring_end'>&quot;</span></span>
  <span class='kw'>end</span>
  
  <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span>
  
  <span class='comment'># feature attribute
</span>  <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
    <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>@ATTRIBUTE </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_f'>f</span><span class='rbrace'>}</span><span class='tstring_content'> </span><span class='tstring_end'>&quot;</span></span>
    <span class='id identifier rubyid_type'>type</span> <span class='op'>=</span> <span class='id identifier rubyid_get_feature_type'>get_feature_type</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span>
    <span class='kw'>if</span> <span class='id identifier rubyid_type'>type</span>
      <span class='kw'>if</span> <span class='id identifier rubyid_type'>type</span> <span class='op'>==</span> <span class='symbol'>:nominal</span>
        <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>{</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_get_feature_values'>get_feature_values</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_uniq'>uniq</span><span class='period'>.</span><span class='id identifier rubyid_sort'>sort</span><span class='period'>.</span><span class='id identifier rubyid_join'>join</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span><span class='rbrace'>}</span><span class='tstring_content'>}</span><span class='tstring_end'>&quot;</span></span>
      <span class='kw'>else</span>
        <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_type'>type</span>
      <span class='kw'>end</span>
    <span class='kw'>else</span> <span class='comment'># treat all other feature types as string
</span>      <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='symbol'>:string</span>
    <span class='kw'>end</span>
  <span class='kw'>end</span>
  
  <span class='comment'># class attribute
</span>  <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>@ATTRIBUTE class {</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_get_classes'>get_classes</span><span class='period'>.</span><span class='id identifier rubyid_join'>join</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span><span class='rbrace'>}</span><span class='tstring_content'>}</span><span class='tstring_end'>&quot;</span></span>
  
  <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span>
  
  <span class='comment'># data header
</span>  <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>@DATA</span><span class='tstring_end'>&quot;</span></span>
  <span class='id identifier rubyid_each_sample'>each_sample</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='comma'>,</span> <span class='id identifier rubyid_s'>s</span><span class='op'>|</span>
    <span class='kw'>if</span> <span class='id identifier rubyid_format'>format</span> <span class='op'>==</span> <span class='symbol'>:sparse</span> <span class='comment'># sparse ARFF
</span>      <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>{</span><span class='tstring_end'>&quot;</span></span>
      <span class='id identifier rubyid_get_features'>get_features</span><span class='period'>.</span><span class='id identifier rubyid_each_with_index'>each_with_index</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
        <span class='kw'>if</span> <span class='id identifier rubyid_s'>s</span><span class='period'>.</span><span class='id identifier rubyid_has_key?'>has_key?</span> <span class='id identifier rubyid_f'>f</span>
          <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_i'>i</span><span class='rbrace'>}</span><span class='tstring_content'> </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_s'>s</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='rbrace'>}</span><span class='tstring_content'>,</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_s'>s</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_zero?'>zero?</span>
        <span class='kw'>else</span> <span class='comment'># missing value
</span>          <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_i'>i</span><span class='rbrace'>}</span><span class='tstring_content'> ?,</span><span class='tstring_end'>&quot;</span></span>
        <span class='kw'>end</span>
      <span class='kw'>end</span>
      <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_get_features'>get_features</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span><span class='rbrace'>}</span><span class='tstring_content'> </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_k'>k</span><span class='rbrace'>}</span><span class='tstring_end'>&quot;</span></span>
      <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>}</span><span class='tstring_end'>&quot;</span></span>
    <span class='kw'>else</span> <span class='comment'># regular ARFF
</span>      <span class='id identifier rubyid_each_feature'>each_feature</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
        <span class='kw'>if</span> <span class='id identifier rubyid_s'>s</span><span class='period'>.</span><span class='id identifier rubyid_has_key?'>has_key?</span> <span class='id identifier rubyid_f'>f</span>
          <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_s'>s</span><span class='lbracket'>[</span><span class='id identifier rubyid_f'>f</span><span class='rbracket'>]</span><span class='rbrace'>}</span><span class='tstring_content'>,</span><span class='tstring_end'>&quot;</span></span>
        <span class='kw'>else</span> <span class='comment'># missing value
</span>          <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_print'>print</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>?,</span><span class='tstring_end'>&quot;</span></span>
        <span class='kw'>end</span>
      <span class='kw'>end</span>
      <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_k'>k</span><span class='rbrace'>}</span><span class='tstring_end'>&quot;</span></span>
    <span class='kw'>end</span>
  <span class='kw'>end</span>
  
  <span class='comment'># close file
</span>  <span class='id identifier rubyid_ofs'>ofs</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span> <span class='kw'>if</span> <span class='kw'>not</span> <span class='id identifier rubyid_ofs'>ofs</span> <span class='op'>==</span> <span class='gvar'>$stdout</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

</div>
    
    <div id="footer">
  Generated on Mon Nov  5 11:19:44 2012 by 
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  0.7.5 (ruby-1.9.3).
</div>

  </body>
</html>